/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.penguin.modules.field.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.penguin.modules.field.entity.FieldCustomer;
import com.thinkgem.penguin.modules.field.service.FieldCustomerService;

/**
 * 商家信息管理维护Controller
 * @author 余小凡
 * @version 2015-05-20
 */
@Controller
@RequestMapping(value = "${adminPath}/field/fieldCustomer")
public class FieldCustomerController extends BaseController {

	@Autowired
	private FieldCustomerService fieldCustomerService;
	
	@ModelAttribute
	public FieldCustomer get(@RequestParam(required=false) String id) {
		FieldCustomer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = fieldCustomerService.get(id);
		}
		if (entity == null){
			entity = new FieldCustomer();
		}
		return entity;
	}
	
	@RequiresPermissions("field:fieldCustomer:view")
	@RequestMapping(value = {"list", ""})
	public String list(FieldCustomer fieldCustomer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FieldCustomer> page = fieldCustomerService.findPage(new Page<FieldCustomer>(request, response), fieldCustomer); 
		model.addAttribute("page", page);
		return "modules/field/fieldCustomerList";
	}

	@RequiresPermissions("field:fieldCustomer:view")
	@RequestMapping(value = "form")
	public String form(FieldCustomer fieldCustomer, Model model) {
		model.addAttribute("fieldCustomer", fieldCustomer);
		return "modules/field/fieldCustomerForm";
	}

	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, @RequestParam(required=false) String type,
			@RequestParam(required=false) Long grade, @RequestParam(required=false) Boolean isAll, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<FieldCustomer> list = fieldCustomerService.findList(new FieldCustomer());
		for (int i=0; i<list.size(); i++){
			FieldCustomer e = list.get(i);
	 				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId",'0');
				map.put("pIds",'0');
				map.put("name", e.getName()); 
				mapList.add(map);
		}
		return mapList;
	}
	
	@RequiresPermissions("field:fieldCustomer:edit")
	@RequestMapping(value = "save")
	public String save(FieldCustomer fieldCustomer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, fieldCustomer)){
			return form(fieldCustomer, model);
		}
		try{
			fieldCustomerService.save(fieldCustomer);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		addMessage(redirectAttributes, "保存商家信息成功");
		return "redirect:"+Global.getAdminPath()+"/field/fieldCustomer/?repage";
	}
	
	@RequiresPermissions("field:fieldCustomer:edit")
	@RequestMapping(value = "delete")
	public String delete(FieldCustomer fieldCustomer, RedirectAttributes redirectAttributes) {
		fieldCustomerService.delete(fieldCustomer);
		addMessage(redirectAttributes, "删除商家信息成功");
		return "redirect:"+Global.getAdminPath()+"/field/fieldCustomer/?repage";
	}

}